home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / role / Ang261Lib.lha / src / gamecheck.c < prev    next >
C/C++ Source or Header  |  1994-10-22  |  4KB  |  174 lines

  1. /*
  2.  * This is the Fun and Games 'front end' for all games.  It checks the load
  3.  * average (15 min) and number of users before allowing play.  The limits are
  4.  * defined in the file RESTRICT_FILE (see header).  If this file does not
  5.  * exist, or the host has no entry, games are derestricted on that machine.
  6.  * Probably best sgid fun, with execute perms for 'others' removed on all
  7.  * games executables.  Create a symbolic link to this named after the game 
  8.  */
  9.  
  10. #include <utmp.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <rpcsvc/rstat.h>
  14. #include <rpcsvc/rusers.h>
  15. #include <errno.h>
  16. #include <sys/file.h>
  17. #include <sys/param.h>
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <pwd.h>
  21. #include "gamecheck.h"
  22.  
  23. char                gamepath[1024];/* Path to game file */
  24. int                 maxusers, maxload;    /* Limits */
  25.  
  26.  
  27. int                 g_access();
  28. void                unlock();
  29. void                log_game();
  30.  
  31. /* Get load average - if this (or the user count) fail, we assume zero */
  32. int 
  33. load_average()
  34. {
  35.     struct statstime    stb;
  36.  
  37.     if (rstat("localhost", &stb) == -1)    /* stats for ourselves */
  38.     return 0;
  39.     else
  40.     return (int)stb.avenrun[2] / FSCALE;    /* Return 15 min load */
  41. }
  42.  
  43. /* Count the number of users */
  44. int 
  45. users()
  46. {
  47.     int count = 0;
  48.  
  49.     if ((count = rnusers("localhost")) == -1)
  50.     return 0;
  51.     else
  52.     return count;
  53. }
  54.  
  55.  
  56. /* Scan the list of directories looking for the game */
  57. int 
  58. game_exists(gamename)
  59. char *gamename;
  60. {
  61.     struct stat buf;
  62.     int         i = 0;
  63.  
  64.     while (strcmp(dir_list[i], "END")) {    /* Go through list of
  65.                          * possibles */
  66.     (void)strcpy(gamepath, dir_list[i]);
  67.     (void)strcat(gamepath, gamename);    /* Tack the name to the
  68.                          * directory */
  69.     if (!stat(gamepath, &buf)) /* Crude, but works */
  70.         return OK_GAME;       /* Okey Dokey, found it */
  71.     i++;
  72.     }
  73.     return NO_GAME;           /* Possibilities exhausted */
  74. }
  75.  
  76.  
  77. int 
  78. restrictions()
  79. {
  80.     FILE *fp;
  81.     char  temphost[10], thishost[10], discard[80];
  82.  
  83.     (void)gethostname(thishost, sizeof thishost);    /* get host */
  84.     if ((fp = my_tfopen(RESTRICT_FILE, "r")) == (FILE *) NULL)
  85.     return 0;           /* No file == no restrictions */
  86.  
  87.     do {
  88.     if (fscanf(fp, "%s%d%d", temphost, &maxload, &maxusers) == EOF)
  89.         return 0;           /* No entry == no restrictions */
  90.     if (temphost[0] == '#')
  91.         (void)fgets(discard, sizeof discard, fp);    /* Comment */
  92.     } while (strcmp(temphost, thishost));    /* Until we've found
  93.                          * ourselves */
  94.  
  95.     (void)fclose(fp);
  96.     return 1;
  97. }
  98.  
  99.  
  100. main(argc, argv)
  101. int    argc;
  102. char **argv;
  103. {
  104.     char *stroke = 0;
  105.     int   count = 0, load = 0;
  106.  
  107. /* Peel off the basename to avoid people like Alfie being awkward */
  108.     if ((stroke = strrchr(argv[0], '/')) != 0)
  109.     argv[0] = stroke + 1;
  110.  
  111.     if (!restrictions())
  112.     goto nochecks;           /* Skip checking - no restrictions file */
  113.  
  114.     if (maxusers == 0 && maxload == 0) {
  115.     (void)printf("Sorry, this machine may not be used for playing games.\n");
  116.     exit(1);
  117.     }
  118.     if ((count = users()) >= maxusers) {
  119.     (void)printf("NO GAMES - TOO MANY USERS\n\n");
  120.     (void)printf("Number of users on this machine   ...... %d\n", count);
  121.     (void)printf("Number of users to disable games  ...... %d\n", maxusers);
  122.     (void)printf("Please try later\n");
  123.     exit(1);
  124.     }
  125.     if ((load = load_average()) >= 300) {
  126.     (void)printf("Current load average ..... %d\n", load);
  127.     (void)printf("So obviously something is fished.\n");
  128.     } else if ((load = load_average()) >= maxload) {
  129.     (void)printf("NO GAMES - LOAD TOO HIGH\n\n");
  130.     (void)printf("Current load average ..... %d\n", load);
  131.     (void)printf("Load to disable games .... %d\n", maxload);
  132.     (void)printf("Please try later\n");
  133.     exit(1);
  134.     }
  135. nochecks:
  136.  
  137.     switch (game_exists(argv[0])) {
  138.  
  139.       case NO_GAME:
  140.     (void)printf("Game doesn't exist - '%s'\n", argv[0]);
  141.     break;
  142.  
  143.       case OK_GAME:
  144.     log_game(argv[0]);
  145.     execv(gamepath, &argv[0]);
  146.     (void)printf("Exec failed - please mail fun\n");
  147.     break;
  148.     }
  149.     exit(1);
  150. }
  151.  
  152. void 
  153. log_game(game)
  154. char *game;
  155. {
  156.     char           user[9];
  157.     struct passwd *userinfo;
  158.     char           buff[256];
  159.     int            fd, uid;
  160.  
  161.     if ((fd = my_topen(GAME_LOG, O_APPEND | O_WRONLY)) < 0) {
  162.     perror(GAME_LOG);
  163.     exit(1);
  164.     }
  165.     if (!(userinfo = getpwuid(uid = getuid()))) {
  166.     perror("");
  167.     exit(1);
  168.     }
  169.     sprintf(buff, "%-8s: %s\n", userinfo->pw_name, game);
  170.  
  171. /* Dunno what would be best to do if we get error - so ignore it :-) */
  172.     write(fd, buff, strlen(buff));
  173. }
  174.